#ifndef __GZ_UART_H__
#define __GZ_UART_H__

#include "eventloop.h"

#include "net_context.h" //NetSendBuffer

#include <string>
#include <queue>
#include <functional>

#include <unistd.h>

namespace Gazella {

typedef std::function<void(const std::string&)> uart_recv_callback_t;

class Uart {
public:
    Uart();
    virtual ~Uart();

    bool open_uart(
        const std::string& dev,
        uint32_t baudrate = 115200,
        uint8_t  parity = 'n',
        uint32_t data_bit = 8,
        uint32_t stop_bit = 1,
        uart_recv_callback_t recv_cb = nullptr
    );
    void close_uart();

    bool send_message(const std::string& message);

    virtual void recv_message(const std::string& message);

private:
    void __recv_cb(ev::io &w, int revents);
    void __send_cb(ev::io &w, int revents);

private:
    int m_fd = 0;

    std::string m_dev;

    ev::io m_rx_io;
    ev::io m_tx_io;

    std::queue<NetSendBuffer> m_tx_buffer_list;

    uart_recv_callback_t m_recv_cb = nullptr;
};
}

#endif